var express = require('express');
var router = express.Router();
var request=require('request');
var cheerio=require('cheerio');
var iconv = require('iconv-lite');
var newsModel=require('../models/news.model');
/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});
//'http://learning.sohu.com/tag/0291/000000291.shtml?qq-pf-to=pcqq.c2c'
router.get('/pachong', function(req, res, next) {
    var url='http://learning.sohu.com/tag/0291/000000291.shtml';
    getBodyObj(url,function(error,$){
        $('.list-box .published' ).each(function(){
            var arr=[];
            arr.push($(this ).find('h3 .content-title a' ).text());
            arr.push($(this ).find('.content-words' ).text());
            arr.push($(this ).find('.time' ).text());
            arr.push('');
            var href=$(this ).find('h3 .content-title a' ).attr('href');
            arr.push(href);
            newsModel.insert(arr,function(err,result){
                if(err){
                    console.log(err.message)
                    res.send(err.message);
                }

            })
        });
        res.send('ok');
    },'GB2312');
});

router.get('/getDetail',function(req,res,next){
    newsModel.getAllUrl(function(error,result){
        result.forEach(function (obj) {
            var url=obj.origin_detail_url;
            getBodyObj(url,function(error,$){
                var content=$("#contentText" ).html();
                newsModel.updateContent(obj.id,content,function(error,result){
                })
            },'GB2312');

        })
        res.send("ok")
    })

})

function getBodyObj(url,callBack,encoding)
{
    try{
        request(url,{encoding: 'binary'},function(error,response,body){
            body = iconv.decode(new Buffer(body, 'binary'), encoding);
            $=cheerio.load(body,{decodeEntities: false});
            callBack(error,$);
        })
    }catch (e){
        console.log(e);
    }

}
/*
process.on('uncaughtException', function (err) {
    console.log(err)
});
*/

module.exports = router;
